package com.example.ddz.framework;

/**
 * Created by IntelliJ IDEA.
 * User: Marcel Melzig
 * Date: 17.08.11
 * Time: 19:32
 */

import java.io.IOException;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

import com.example.ddz.framework.impl.AndroidFileIO;
import com.example.ddz.framework.impl.AndroidInput;
import com.example.ddz.framework.impl.Audio;

/**
 * This class ties all handlers together to the game framework.
 */
public class AndroidGame extends Activity implements IGame
{
    private AndroidFastRenderView renderView;
    private IGraphics graphics;
    private IAudio audio;
    private IInput input;
    private IFileIO fileIO;
    private Screen screen;
    //WakeLock wakeLock;

    /**
     * Instantiates an AndroidGame instance which encapsulates the functionality of the
     * framework.
     *
     * @param savedInstanceState The saved instance state.
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;

        //Creates a framebuffer in the size of the devices display and passes a scale factor to the rendering
        //classes to scale the source textures to the correct size.
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int frameBufferWidth = metrics.widthPixels;
        int frameBufferHeight = metrics.heightPixels;
        Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, frameBufferHeight, Config.RGB_565);
        float scaleX = 1.0f;
        float scaleY = 1.0f;

        try
        {
            Bitmap backgroundImage = BitmapFactory.decodeStream(getAssets().open("background.png"));
            scaleX = (float) frameBuffer.getWidth() / (float) (isLandscape ? backgroundImage.getHeight() : backgroundImage.getWidth());
            scaleY = (float) frameBuffer.getHeight() / (float) (isLandscape ? backgroundImage.getWidth() : backgroundImage.getHeight());
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        renderView = new AndroidFastRenderView(this, frameBuffer);
        graphics = new AndroidGraphics(getAssets(), frameBuffer, scaleX, scaleY);
        fileIO = new AndroidFileIO(getAssets());
        audio = new Audio(this);
        input = new AndroidInput(this, renderView);
        screen = GetStartScreen();
        setContentView(renderView);
        /*PowerManager powerManager = (PowerManager)
                getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");*/
    }

    /**
     * Gets called when the game resumes.
     */
    @Override
    public void onResume()
    {
        super.onResume();
        //wakeLock.acquire();
        screen.Resume();
        renderView.Resume();
    }

    /**
     * Gets called when the game pauses.
     */
    @Override
    public void onPause()
    {
        super.onPause();
        //wakeLock.release();
        renderView.Pause();
        screen.Pause();
        if (isFinishing())
            screen.Dispose();
    }

    /**
     * Gets the frameworks input instance.
     *
     * @return The input instance.
     */
    @Override
    public IInput GetInput()
    {
        return input;
    }

    /**
     * Gets the FileIO instance.
     *
     * @return The FileIO instance.
     */
    @Override
    public IFileIO GetFileIo()
    {
        return fileIO;
    }

    /**
     * Gets the Graphics instance.
     *
     * @return The graphics instance.
     */
    @Override
    public IGraphics GetGraphics()
    {
        return graphics;
    }

    /**
     * Gets the audio instance.
     *
     * @return The audio instance.
     */
    @Override
    public IAudio GetAudio()
    {
        return audio;
    }

    /**
     * Sets a screen.
     *
     * @param screen The screen which should be displayed.
     */
    @Override
    public void SetScreen(Screen screen)
    {
        if (screen == null)
            throw new IllegalArgumentException("Screen must not be null");
        this.screen.Pause();
        this.screen.Dispose();
        screen.Resume();
        screen.Update(0);
        this.screen = screen;
    }

    /**
     * Gets the current screen.
     *
     * @return The current screen.
     */
    public Screen GetCurrentScreen()
    {
        return screen;
    }

    /**
     * Gets the start screen.
     *
     * @return The start screen.
     */
    public Screen GetStartScreen()
    {
        return screen;
    }
}
